<template>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          clearable
          :maxlength="50"
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="场次名称" prop="sessionName">
        <el-input
          clearable
          :maxlength="50"
          v-model="queryParams.sessionName"
          placeholder="请输入场次名称"
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="履约状态" prop="status">
        <el-select clearable v-model="queryParams.status" placeholder="请选择" class="!w-200px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BID_HANDLE_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-date-picker
          v-model="queryParams.updateTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          type="datetimerange"
          class="!w-400px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['bid:agreement:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          导出
        </el-button>
        <el-button
          type="primary"
          plain
          @click="handleHaulCapacity"
          :loading="haulCapacityLoading"
          v-hasPermi="['bid:agreement:haulCapacity']"
        >
          获取拉运量
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      border
      stripe
      :header-cell-style="{ background: '#F3F4F7', color: '#555' }"
    >
      <el-table-column label="编号" align="center" width="60" fixed="left">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="客户名称"
        align="center"
        prop="customerName"
        min-width="150"
        fixed="left"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="场次名称"
        align="center"
        prop="sessionName"
        min-width="150"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="履约状态" align="center" prop="status" width="100">
        <template #default="{ row }">
          <dict-tag :type="DICT_TYPE.BID_HANDLE_STATUS" :value="row.status" />
        </template>
      </el-table-column>
      <el-table-column label="拍中标段" align="center" prop="bidNumber" width="100" />
      <el-table-column label="产品名称" align="center" prop="coalType" width="150" />
      <el-table-column label="中标价格(元/吨)" align="center" prop="bidPrice" width="130" />
      <el-table-column label="成交量(吨)" align="center" prop="turnover" width="100" />
      <el-table-column label="拉运量(吨)" align="center" prop="transportAmount" width="100" />
      <el-table-column label="已缴保证金(元)" align="center" prop="depositPaid" width="130" />
      <el-table-column label="当前保证金(元)" align="center" prop="currentPaid" width="130" />
      <el-table-column label="已退保证金(元)" align="center" prop="returnedPaid" width="130" />
      <el-table-column label="扣款(元)" align="center" prop="deductMoney" width="100" />
      <el-table-column
        label="扣款原因"
        align="center"
        prop="deductReason"
        min-width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="中标时间"
        align="center"
        prop="bidTime"
        min-width="200"
        :formatter="dateFormatter"
      />
      <el-table-column label="交货周期" align="center" prop="deliveryCycle" min-width="200" />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        min-width="200"
        :formatter="dateFormatter"
      />
      <el-table-column label="操作" align="center" min-width="100" fixed="right">
        <template #default="{ row }">
          <el-button
            v-if="row.status === 1"
            link
            type="primary"
            v-hasPermi="['bid:agreement:handle']"
            @click="handleFn(row)"
            >处理</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <HandleModel ref="handleModel" @success="success"></HandleModel>
</template>
<script setup lang="ts" name="mySubject">
import * as Utils from '@/utils/dict'
import download from '@/utils/download'
import * as performance from '@/api/performance'
import { dateFormatter } from '@/utils/formatTime'
import HandleModel from './handleModel.vue'

const { DICT_TYPE, getIntDictOptions } = reactive(Utils)
const loading = ref(true) // 列表的加载中
const exportLoading = ref(false) // 列表的加载中
const haulCapacityLoading = ref(false) // 列表的加载中
const message = useMessage()
const {t} = useI18n()
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  sessionName: undefined,
  customerName: undefined,
  status: undefined,
  updateTime: []
})
const queryFormRef = ref()
const handleModel = ref()

/** 查询列表 */

const getList = async () => {
  loading.value = true
  try {
    const data = await performance.getAgreementManageList(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const success = () => {
  handleQuery()
}

const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await performance.exportAgreementExcel(queryParams)
    download.excel(data, '履约管理列表.xls')
  } finally {
    exportLoading.value = false
  }
}
const handleHaulCapacity = async () => {
  try {
    haulCapacityLoading.value = true
    await performance.getTransportAmount()
    message.success(t('common.operateSuccess'))
    handleQuery()
    haulCapacityLoading.value = false
  } finally {
    haulCapacityLoading.value = false
  }
}

const handleFn = (row) => {
  handleModel.value.open(row)
}

/** 初始化 **/
onMounted(() => {
  getList()
})

defineExpose({})
</script>
